package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.RepairListDTO;
import com.huaxin.common.Entity.RepairRecord;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.view.pulltorefresh.extras.recyclerview.PullToRefreshRecyclerView;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/29.
 */

public class HxRepairRecordActivity extends BaseActivity {

    @BindView(R.id.more)
    TextView                  more;
    @BindView(R.id.toolbar)
    Toolbar                   toolbar;
    @BindView(R.id.pull_view)
    PullToRefreshRecyclerView pullView;
    @BindView(R.id.content)
    FrameLayout               content;
    RecyclerView listView;
    List _datas = new ArrayList<>();
    MyAdapter adapter;
    private String _deviceCode;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_repair_record;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        toolbar.setTitle("维修记录");

        pullView.setLayoutVisibilityEnable(false);
        listView = pullView.getRefreshableView();
        listView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
                false));
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
    }


    @Override
    public void requestData() {
        ApiRequestData(LOADING);
    }

    public static void start(Context context, String deviceCode) {
        Intent intent = new Intent(context, RepairRecordActivity.class);
        intent.putExtra(Constants.IK_DEVICE_CODE, deviceCode);
        context.startActivity(intent);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (extras.containsKey(Constants.IK_DEVICE_CODE)) {
            _deviceCode = extras.getString(Constants.IK_DEVICE_CODE);
        } else {
            finish();
        }
    }

    protected class MyAdapter extends RecyclerView.Adapter {

        private static final int ITEM_EMPTY       = 0x00;
        private static final int ITEM_TYPE_RECORD = 0x01;


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == ITEM_EMPTY) {
                return new EmptyHolder(mInflater.inflate(R.layout.item_empty_nodata_center, parent, false));
            } else if (viewType == ITEM_TYPE_RECORD) {
                return new MyViewHolder(mInflater.inflate(R.layout.item_repair_record, parent,
                        false));
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (getItemViewType(position) == ITEM_TYPE_RECORD) {
                ((MyViewHolder) holder).update(position);
            }
        }

        @Override
        public int getItemViewType(int position) {
            Object data = _datas.get(position);
            if (data instanceof RepairRecord) {
                return ITEM_TYPE_RECORD;
            }
            return ITEM_EMPTY;
        }


        @Override
        public int getItemCount() {
            return _datas == null ? 0 : _datas.size();
        }


        public class EmptyHolder extends RecyclerView.ViewHolder {
            View itemView;

            public EmptyHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                autoClose(ButterKnife.bind(this, itemView));
                this.itemView = itemView;
                //ImageMarkUtil.setWaterMarkTextBg(itemView,ArrangeTaskHistoryActivity.this,"暂无数据");
            }

            public void update(int position) {


            }
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_date)
            TextView     tvDate;
            @BindView(R.id.tv_worker)
            TextView     tvWorker;
            @BindView(R.id.ls_img)
            RecyclerView lsImg;
            @BindView(R.id.tv_des)
            TextView     tvDes;

            ImgAdapter   iAdapter;
            List<String> imgs;
            int          position;
            View         itemView;

            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                this.itemView = itemView;
                imgs = new ArrayList<>();
                iAdapter = new ImgAdapter();
                lsImg.setLayoutManager(new LinearLayoutManager(HxRepairRecordActivity.this, LinearLayoutManager.HORIZONTAL, false));
                lsImg.setAdapter(iAdapter);
            }


            public void update(int position) {
                this.position = position;
                RepairRecord repairRecord = (RepairRecord) _datas.get(position);
                loadText(tvDate, dateFormat(repairRecord.getFinishDate(), "yyyy-MM-dd HH:mm"));
                loadText("备注：", tvDes, repairRecord.getResolvent());
                loadText(tvWorker, repairRecord.getWorkerName());
                imgs.clear();
                if (repairRecord.getImages() == null || repairRecord.getImages().isEmpty()) {
                    imgs.add(Constants.EMPTY_VIEW);
                } else {
                    imgs.addAll(repairRecord.getImages());
                }
                iAdapter.setImages(imgs);
                iAdapter.notifyDataSetChanged();
            }

        }
    }

    private void ApiRequestData(int status) {
        updateLoadStatus(status);
        Call<RepairListDTO> call = HxTransmitRequestHelper.getRequestApi().repairRecord(_deviceCode);
        autoClose(call);
        call.enqueue(new Callback<RepairListDTO>() {
            @Override
            public void onResponse(Call<RepairListDTO> call, Response<RepairListDTO> response) {
                RepairListDTO repairListDTO = response.body();
                if (response.isSuccessful() && repairListDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    if (repairListDTO.isSuccessful()) {
                        _datas.clear();
                        if (repairListDTO.getWorkTaskRecord() == null
                                || repairListDTO.getWorkTaskRecord().isEmpty()) {
                            _datas.add(Constants.EMPTY_VIEW);
                        } else {
                            _datas.addAll(repairListDTO.getWorkTaskRecord());
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        updateLoadStatus(LOAD_SUCCESS);
                    }
                } else {
                    updateLoadStatus(LOAD_SUCCESS);
                }

            }

            @Override
            public void onFailure(Call<RepairListDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }


    protected class ImgAdapter extends RecyclerView.Adapter {
        List<String> mImages;
        private static final int ITEM_TYPE_EMPTY   = 0x02;
        private static final int ITEM_TYPE_IMG     = 0x01;
        private static final int ITEM_TYPE_DEFAULT = 0x00;

        public void setImages(List<String> images) {
            this.mImages = images;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == ITEM_TYPE_IMG) {
                return new ImgHolder(mInflater.inflate(R.layout.item_h_image, parent, false));
            } else if (viewType == ITEM_TYPE_EMPTY) {
                return new EmptyHolder(mInflater.inflate(R.layout.item_empty3, parent, false));
            }
            return null;

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (getItemViewType(position) == ITEM_TYPE_IMG) {
                ((ImgHolder) holder).update(position);
            } else if (getItemViewType(position) == ITEM_TYPE_EMPTY) {
                ((EmptyHolder) holder).update(position);
            }

        }

        @Override
        public int getItemViewType(int position) {
            Object data = mImages.get(position);
            if (data.equals(Constants.EMPTY_VIEW)) {
                return ITEM_TYPE_EMPTY;
            }
            return ITEM_TYPE_IMG;
        }

        @Override
        public int getItemCount() {
            return mImages == null ? 0 : mImages.size();
        }


        public class ImgHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_img)
            ImageView ivImg;
            View itemView;

            public ImgHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                this.itemView = itemView;
            }

            public void update(int position) {
                loadNetImg(ivImg, mImages.get(position));
            }
        }

        public class EmptyHolder extends RecyclerView.ViewHolder {
            View itemView;

            public EmptyHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                this.itemView = itemView;
            }

            public void update(int position) {

            }
        }

    }

}